﻿namespace Sai.Core.Introspection
{
	using System;
	using System.Collections.Generic;
	using System.Linq;

	public class TypeDeclarationCollection : System.Collections.ObjectModel.Collection<ITypeDeclaration>
	{
		public ICollection<TypeName> InCommonWith(IEnumerable<ITypeDeclaration> other)
		{
			if (other == null)
			{
				throw new ArgumentNullException("other");
			}

			var intersection = this.Intersect(other, new TypeDeclarationNameComparer());

			var commonTypes = new List<TypeName>();

			if (intersection.Any())
			{
				foreach (var common in intersection)
				{
					commonTypes.Add(common.TypeName);
				}
			}

			commonTypes.Sort();

			return commonTypes;
		}

		/// <summary>
		/// Types that are in this collection that are not in other.
		/// </summary>
		/// <param name="other">Collection to compare against</param>
		/// <returns>List of types not found in other collection</returns>
		public ICollection<TypeName> DifferencesFrom(IEnumerable<ITypeDeclaration> other)
		{
			if (other == null)
			{
				throw new ArgumentNullException("other");
			}

			var setDifference = this.Except(other, new TypeDeclarationNameComparer());
			var differenceTypes = new List<TypeName>();

			if (setDifference.Any())
			{
				foreach (var difference in setDifference)
				{
					differenceTypes.Add(difference.TypeName);
				}
			}

			differenceTypes.Sort();

			return differenceTypes;
		}
	}
}
